#include "cppTutorLinkedListNew.h"

List :: List () : head (NULL), curr (NULL), temp (NULL)
{
}

List :: ~List ()
{
}

void List :: AddNode (int addData)
{
    nodePtr n = new node;
    n->next = NULL; //initially set it to the last element
    n->data = addData; // takes the data and store in the node

    // general case : assuming that list is already created

    if (head != NULL) // list is intact and we have some data here
    {
        curr = head; // set the head to the current of the list
        while (curr->next != NULL) // check that we are at the in the last of the node
        {
             curr = curr->next;
        }
        curr->next = n; // now we are at the last of the list, currently n->next is pointing to the NULL
    }
    else // if we don't have a list created clready
    {
        head = n; // new node that we created
    }
}

void List :: DeleteData (int delData)
{
    nodePtr delPtr = NULL; // create a pointer that will perform the deletion of the requested noted
    
    temp = head; // create the temp pointer and point to the beginning of the list
    curr = head; // create the current pointer and point to the neginning of the list

    // check to see if the curr node is the one we want to delete, if it is then we delete 
    // if not then we advance the curr pointer but we are going to have the temp pointer trail the curr pointer 
    // one step behind as it advances every time so that we patch up the list if we delete a node in the mid of the list

    // check the curr pointer is not poiting to the NULL
    while (curr != NULL && curr->data != delData) // if got then exit and delete as we got the node to delete
    {
        // traverse the list
        temp = curr;
        curr = curr->next;
    }// either we pass the lost or we got the data to delete

    if (curr == NULL) // complete list passed
    {
        cout << delData << " is not in the list !!!" << endl;
        delete delPtr; // remove the pointer
    }
    else // condition when while loop exited , means we found the data
    {
        delPtr = curr; // del pointer is pointing to the node we want to delete
        curr = curr->next; // advances the curr pointer to the next node
        temp->next = curr; //  since we trail the temp pointer one step behind the curr pointer, the temp pointer is pointing to the node that we want to connect to the node that the curr is pointing now, it patches the list

        // check for deletion of head element also, we need to repoint the head node
        if (delPtr == head)
        {
            head = head->next;
            temp = NULL;
        }

        delete delPtr; // delete the node that we want to delete
        cout << delData << " is delete from the list !!!" << endl;
    }
}

void List :: printList  ()
{
    curr = head;
    while (curr != NULL)
    {
        cout << "[" << curr->data << "]" << endl;
        curr = curr->next;
    }
}